package com.frejoys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.dto.app.OrderAddressUpDto;
import com.frejoys.dao.entity.OrderAddress;
import com.frejoys.dao.mapper.OrderAddressMapper;
import com.frejoys.service.OrderAddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

// 广告
@Service
@RequiredArgsConstructor
public class OrderAddressServiceImpl extends ServiceImpl<OrderAddressMapper, OrderAddress> implements OrderAddressService {

    @Override
    public boolean addInfo(OrderAddress orderAddress) {
        return save(orderAddress);
    }

    @Override
    public OrderAddress getInfo(Integer id) {
        LambdaQueryWrapper<OrderAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderAddress::getId, id);
        return getOnly(wrapper);
    }

    @Override
    public boolean upInfo(OrderAddressUpDto dto) {

        LambdaUpdateWrapper<OrderAddress> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OrderAddress::getId, dto.getId());

        if (StrUtil.isNotEmpty(dto.getName())) {
            wrapper.set(OrderAddress::getName, dto.getName());
        }

        if (StrUtil.isNotEmpty(dto.getPhone())) {
            wrapper.set(OrderAddress::getPhone, dto.getPhone());
        }

        if (StrUtil.isNotEmpty(dto.getProvince())) {
            wrapper.set(OrderAddress::getProvince, dto.getProvince());
        }

        if (StrUtil.isNotEmpty(dto.getCity())) {
            wrapper.set(OrderAddress::getCity, dto.getCity());
        }

        if (StrUtil.isNotEmpty(dto.getDistrict())) {
            wrapper.set(OrderAddress::getDistrict, dto.getDistrict());
        }

        if (StrUtil.isNotEmpty(dto.getStreet())) {
            wrapper.set(OrderAddress::getStreet, dto.getStreet());
        }

        if (StrUtil.isNotEmpty(dto.getAddress())) {
            wrapper.set(OrderAddress::getAddress, dto.getAddress());
        }

        return update(wrapper);
    }
}